
package com.rd.student.jike.class4.wanghaixin.test0719;

import java.util.concurrent.locks.ReentrantLock;

public class TestBank2 extends Thread{
    Account2 account;
    double money;
    public TestBank2(String name,Account2 account, double money){
        super(name);
        this.account = account;
        this.money = money;
    }

    public void run(){
        account.drawMoney(money);
    }

    public static void main(String[] args) throws InterruptedException {
        Account2 account2 = new Account2("123456",390);
        new TestBank2("lisi",account2,120).start();
        new TestBank2("wangwu",account2,120).start();
        new TestBank2("zhangsan",account2,164).start();
        sleep(1000);
        System.out.println(account2.getBalance());
    }

}

class Account2{
    private String accountID;
    private double balance;
    public Account2(){}
    ReentrantLock lock = new ReentrantLock();
    public Account2(String accountID,double balance){
        this.accountID = accountID;
        this.balance = balance;
    }

    public String getAccountID() {
        return accountID;
    }

    public void setAccountID(String accountID) {
        this.accountID = accountID;
    }

    public double getBalance() {
        return balance;
    }

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void drawMoney(double money){
        lock.lock();
        if(balance - money<0){
            System.out.println(Thread.currentThread().getName()+"不能取钱余额不足");
        }else{
            System.out.println(Thread.currentThread().getName()+"可以取钱");
            setBalance(balance-money);
            System.out.println("取钱后的余额为"+balance);
        }
        lock.unlock();

    }
}